if (sessionStorage.state === undefined) {
    sessionStorage.state = "{}"
}
if (localStorage.select === undefined) {
    localStorage.select = "[]"
}

import {md5} from "../assets/commonjs/md5";

export const $aos = {
    account: function (userName) {
        if (userName !== undefined) {
            let session = JSON.parse(sessionStorage.state);
            session.userName = userName;
            sessionStorage.state = JSON.stringify(session);
        } else {
            let session = JSON.parse(sessionStorage.state);
            return session.userName;
        }
    },
    token: function (token) {
        if (token !== undefined) {
            let session = JSON.parse(sessionStorage.state);
            session.token = token;
            sessionStorage.state = JSON.stringify(session);
        } else {
            let session = JSON.parse(sessionStorage.state);
            return session.token;
        }
    },
    setItem: function (key, value) {
        let session = JSON.parse(sessionStorage.state);
        session[key] = value;
        sessionStorage.state = JSON.stringify(session);
    },
    getItem: function (key) {
        return new Promise(resolve => {
            let session = JSON.parse(sessionStorage.state);
            resolve(session[key]);
        })
    },
    removeItem: function (key) {
        let session = JSON.parse(sessionStorage.state);
        delete session[key];
        sessionStorage.state = JSON.stringify(session);
    },
    clear(){
        sessionStorage.state = '{}';
    },
    setSelect: function (val) {
        let session = JSON.parse(localStorage.select);
        if (session.length !== 0) {
            (function (callback) {
                let j = true;
                for (let i = 0; i < session.length; i++) {
                    if (session[i].value === val) {
                        j = false;
                    }
                }
                callback(j);
            })(res => {
                if (res) {
                    session.push({value: val});
                }
            });
        } else {
            session.push({value: val});
        }
        localStorage.select = JSON.stringify(session);
    },
    getSelect: function (callback) {
        let session = JSON.parse(localStorage.select);
        callback(session);
    }
};

export const $role = {
    auth: function (roleAuth) {
        if (roleAuth !== undefined) {
            let session = JSON.parse(sessionStorage.state);
            let num = Math.floor(Math.random() * 99999999999).toString();
            session.role = md5(num) + roleAuth;
            sessionStorage.state = JSON.stringify(session);
        } else {
            let session = JSON.parse(sessionStorage.state);
            let r = parseInt(session.role.substr(-1));
            return r === 0 ? 10 : r;
        }
    }
};

export function $(name) {
    $.fn = {
        init: function () {
            let session = JSON.parse(sessionStorage.state);
            this.set = (value) => {
                session[name] = value;
            };
            this.get = (callback) => {
                if (callback) {
                    callback(session[name]);
                } else {
                    return session[name];
                }
            };
            this.remove = () => {
                delete session[name];
            };
            this.clear = () => {
                session.state = {};
            };
            sessionStorage.state = JSON.stringify(session);
        }
    };
    return new $.fn.init(name);
}